<template>
    <div>
        <div class="search-wrapper">
        <!--     <i-select v-model="sceneRist" @on-change="changeSceneRist">
                <i-option value="low">低危</i-option>
                <i-option value="medium">中危</i-option>
                <i-option value="high">高危</i-option>
            </i-select>
            <Input
                v-model.trim="teamName"
                clearable
                placeholder="团队名称"
                @on-change="searchTeamName"
            />
            <Button @click="resetSearchAlarm"
                ><i class="el-icon-refresh-right"
            /></Button>-->
        </div>
        <TableList
            :columns="columns"
            :data="data"
            :total="total"
            :page.sync="query.pageIndex"
            :size.sync="query.pageSize" 
            @queryFun="getList"
        >
        </TableList>
    </div>
</template>
<script>
    import { TimeFormatter } from '@/libs/utils';
    import {
        vulnerabilityStats,
        pushVulnerability
    } from '@/api/referee.js';
    export default {
        name:'VulnerabilityStats',
        props:['sceneId'],
        data(){
            return {
                query:{
                    testId:this.sceneId,
                    pageIndex:1,
                    pageSize:10
                },
                data:[],
                total:0,
                columns:[
                    {
                        title:'漏洞名称',
                        key:'name',
                        tooltip:true
                    },
                    {
                        title:'漏洞状态',
                        key:'status',
                        tooltip:true,
                        filter:(status) => this.filterStatus(status).text,
                        setColor:(status) => this.filterStatus(status).color,
                    },
                    {
                        title:'漏洞等级',
                        key:'level',
                        tooltip:true,
                        filter:(level) => this.filterLevel(level).text,
                        setColor:(level) => this.filterLevel(level).color,
                    },
                    {
                        title:'发现时间',
                        key:'created',
                        tooltip:true
                    },
                    {
                        title:'推送状态',
                        key:'pushStatus',
                        tooltip:true,
                        filter:(pushStatus) => pushStatus ? '已推送' : '未推送',
                        setColor:(pushStatus) => pushStatus ? 'var(--status-yes)' : 'var(--status-no)',
                    },
                    {
                        title:'推送时间',
                        key:'pushTime',
                        tooltip:true
                    },
                    {
                        title:'反馈状态',
                        key:'feedbackStatus',
                        tooltip:true,
                        filter:(feedbackStatus) => feedbackStatus ? '已反馈' : '未反馈',
                        setColor:(feedbackStatus) => feedbackStatus ? 'var(--status-yes)' : 'var(--status-no)',
                    },
                    // {
                    //     title:'反馈时间',
                    //     key:'feedbackTime',
                    //     tooltip:true
                    // },
                    {
                        title:'复测时间',
                        key:'retestTime',
                        tooltip:true
                    },
                    {
                        title:'归属靶标',
                        key:'targetName',
                        tooltip:true
                    },
                    {
                        title:'操作',
                        width:100,
                        key:'action',
                        opList:[
                            {
                                txt:"推送",
                                disabled:(data) => data.pushStatus == 1,
                                fun:(data) => {
                                    this.pushVulnerability(data.id);
                                }
                            }
                        ]
                    }
                ]
            }
        },
        mounted(){
            this.getList()
        },
        methods:{
            getList(){
                vulnerabilityStats(this.query).then(({data:{code,data,message}}) => {
                    if(code === 1){
                        console.log(data)
                        this.data = data.items;
                        this.total = data.total;
                    }else{
                        this.$Message.error(message)
                        this.data = [];
                        this.total = 0;
                    }
                }).catch(() => {
                    this.$Message.error('获取列表失败')
                })
            },
            pushVulnerability(id){
                pushVulnerability(id,{pushStatus:1,pushTime:TimeFormatter.time(new Date().getTime())}).then(({data:{code,data,message}}) => {
                    if(code === 1){
                        this.getList();
                    }else{
                        this.$Message.error(message)
                    }
                }).catch(() => {
                    this.$Message.error('推送失败')
                })
            },
            filterStatus(status = 1){
                let color = '';
                let text = '';
                if (status == 3) {
                    color = 'var(--status-yes)';
                    text = '修复中';
                } else if (status == 2) {
                    color = 'var(--status-yes)';
                    text = '已复测';
                } else if (status == 1) {
                    color = 'var(--status-no)';
                    text = '未修复';
                } else if (status == 4) {
                    color = 'var(--status-pre)';
                    text = '复测中';
                } else if (status == 5) {
                    color = 'var(--status-pre)';
                    text = '已修复';
                }
                return {
                    color,
                    text
                }
            },
            filterLevel(val) {
                switch(val) {
                    case '0': return { text: '高危', color: '#ed4014' }
                    case '1': return { text: '中危', color: '#ff9900' }
                    case '2' || '3': return { text: '低危', color: '#2db7f5' }
                    default: return { text: '未定级', color: '' }
                }
            }
        }
    }
</script>
<style scoped>
    .search-wrapper {
        margin: 15px 0;
        display: flex;
        justify-content: flex-end;
    }
</style>